package com.gt.wxpay.cashier.payment.application.impl;

import com.gt.wxpay.cashier.domain.model.CashierPayment;
import com.gt.wxpay.cashier.domain.model.PaymentStatusEnum;
import com.gt.wxpay.cashier.domain.model.RefundStatusEnum;
import com.gt.wxpay.cashier.domain.model.TransactionTypeEnum;
import com.gt.wxpay.cashier.payment.application.CheckCommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * packageName com.gt.wxpay.cashier.payment.application.impl
 *
 * @author GuoTong
 * @version JDK 8
 * @className CheckCommonServiceImpl
 * @date 2024/3/16
 * @description 公共检测服务
 */
@Service
public class CheckCommonServiceImpl implements CheckCommonService {

    @Override
    public String payOrderCreateCheck(CashierPayment cashierPayment) {
        String orderNo = cashierPayment.getOrderNo();
        if (orderNo == null || orderNo.isEmpty()) {
            return "msg：业务系统的订单编号 不能为空";
        }
        String appId = cashierPayment.getAppId();
        if (appId == null || appId.isEmpty()) {
            return "msg：支付的appId 不能为空";
        }
        String description = cashierPayment.getDescription();
        if (description == null || description.isEmpty()) {
            return "msg：订单描述 不能为空";
        }
        String notifyUrl = cashierPayment.getNotifyUrl();
        if (notifyUrl == null || notifyUrl.isEmpty()) {
            return "msg：支付结果通知url 不能为空";
        }
        Long amount = cashierPayment.getAmount();
        if (amount == null) {
            return "msg：支付金额 不能为空";
        }
        String userId = cashierPayment.getUserId();
        if (userId == null || userId.isEmpty()) {
            return "msg：公众用户id 不能为空";
        }
        String channelPayType = cashierPayment.getChannelPayType();
        if (channelPayType == null || channelPayType.isEmpty()) {
            return "msg：渠道类型(JSAPI, NATIVE) 不能为空";
        }
        String payChannel = cashierPayment.getPayChannel();
        if (payChannel == null || payChannel.isEmpty()) {
            return "msg：支付类型(微信支付，支付宝支付) 不能为空";
        }
        String phoneNo = cashierPayment.getPhoneNo();
        if (phoneNo == null || phoneNo.isEmpty()) {
            return "msg：手机号 不能为空";
        }
        String merchantId = cashierPayment.getMerchantId();
        if (merchantId == null || merchantId.isEmpty()) {
            return "msg：商户号 不能为空";
        }
        String goodsType = cashierPayment.getGoodsType();
        if (goodsType == null || goodsType.isEmpty()) {
            return "msg：商品类型 不能为空";
        }
        return null;
    }

    @Override
    public String payOrderCloseCheck(CashierPayment cashierPayment) {
        String channelFlowNo = cashierPayment.getChannelFlowNo();
        if (StringUtils.isEmpty(channelFlowNo)) {
            return "msg：渠道流水号 不能为空";
        }
        Integer status = cashierPayment.getStatus();
        if (!Objects.equals(status, PaymentStatusEnum.UNPAY.code())) {
            return "msg：订单不是未支付或者支付中 不能关闭";
        }
        String merchantId = cashierPayment.getMerchantId();
        if (StringUtils.isEmpty(merchantId)) {
            return "msg：商户号 不能为空";
        }
        return null;
    }

    @Override
    public String payOrderRefundCheck(CashierPayment cashierPayment) {
        String paymentNo = cashierPayment.getPaymentNo();
        if (StringUtils.isEmpty(paymentNo)) {
            return "msg：退款单号(原支付订单号) 不能为空";
        }
        String notifyUrl = cashierPayment.getNotifyUrl();
        if (notifyUrl == null || notifyUrl.isEmpty()) {
            return "msg：退款结果通知url 不能为空";
        }
        Long amount = cashierPayment.getRefundAmount();
        if (amount == null) {
            return "msg：退款金额 不能为空";
        }
        return null;
    }

    @Override
    public String checkRefundInMerchant(CashierPayment cashierPayment) {
        if (!cashierPayment.getTransactionType().equals(TransactionTypeEnum.REFUND.code())) {
            return "订单" + cashierPayment.getPaymentNo() + "不是退款订单";
        }
        if (!cashierPayment.getStatus().equals(RefundStatusEnum.SUCCESS.getCode())) {
            return "订单" + cashierPayment.getPaymentNo() + "已经退款成功";
        }
        return null;
    }
}
